#!/usr/bin/perl

# Build OODoc in a workdir (like tmp/build), then point Module::Build
# to those .pm and .pod files via the pm_files and pod_files
# directives. Otherwise it's a normal build process.

use warnings;
use strict;

use 5.008;
use lib './lib';  # for OODoc

use Module::Build;
use File::Path;
use File::Spec;
use File::Find;

my $workdir           = '.';
my $distribution      = 'sgftools';
my $dist_version_from = 'lib/Waya/SGF.pm';

my $dist_version = Module::Build->version_from_file(
    File::Spec->catfile(split '/', $dist_version_from));

# XXX: commented out OODoc stuff so Build can see changes in the source tree
# and copy them over to _build if necessary. With OODoc, files in lib
#
# use OODoc;
# my $workdir = 'tmp/build';
# mkdoc();

print "Running Module::Build...\n";
#  XXX: wouldn't be so smart to leave rmtree in when $workdir is './lib'
#
# Module::Build->subclass(
#     class => 'My::Builder',
#     code  => qq{
#          use File::Path;
#          sub ACTION_realclean {
#              my \$self = shift;
#              print "Deleting $workdir\n";
#              \$self->SUPER::ACTION_realclean(\@_);
#              rmtree("$workdir");
#              unlink('Makefile');
#         }
#     })->new(

Module::Build->new(
        module_name  => $distribution,
        dist_version => $dist_version,
        license      => 'perl',
        pm_files     => find_files(pm  => $workdir),
        pod_files    => find_files(pod => $workdir),
        script_files => find_script_files(),
        test_files   => 't/{*/,}*.t',
        requires     => {
            'perl'                 => '5.8.0',
            'Regexp::Common'       => 2.118,
            'Parse::RecDescent'    => 1.94,
            'Error'                => 0.15,
            'YAML'                 => 0.35,
        },
    )->create_build_script;



sub mkdoc {
    print "Creating documentation...\n";

    -d $workdir
    || mkpath($workdir)
    || die "Cannot create directory $workdir: $!\n";

    my $doc  = OODoc->new(
        distribution => $distribution,
        version      => $dist_version,
    );

    $doc->processFiles(
        workdir => $workdir,
        version => $dist_version,
        select  => qr!^lib.*\.pm$!,
        parser_args => { dont_write_version => 1 },
    );

    $doc->prepare;
    $doc->create
      ( 'pod',
      , workdir => $workdir
      , append  => sub {
            my ($name, $self, %args) = @_;
            my $package = $args{manual}{OP_package};
            $args{output}->print(<<TEXT) });

=head1 BUGS

If you find any bugs or oddities, please do inform the authors.

=head1 INSTALLATION

See perlmodinstall for information and options on installing Perl modules.

=head1 AVAILABILITY

The latest version of this module is available from the CVS repository
on makabaer.cc.univie.ac.at. Use the following command to check out the
latest distribution:

  cvs co Registry

=head1 VERSION

This document describes version $dist_version of C<$package>.

=head1 AUTHOR

Written by:

=over 4

=item *

Marcel GrE<uuml>nauer <marcel.gruenauer\@univie.ac.at>

=item *

Florian Helmberger <florian.helmberger\@univie.ac.at>

=item *

Mark Hofstetter <mark.hofstetter\@univie.ac.at>

=back

See the Changes file for other contributors.

=head1 COPYRIGHT

Copyright 2003 NIC.at. All rights reserved.

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

TEXT

}


sub find_files {
    my ($suffix, $workdir) = @_;
    my $worklib = "$workdir/lib";
    my %files;
    find(sub {
        return unless -f && /\.$suffix$/;
        (my $distloc = $File::Find::name) =~ s/^$worklib/lib/;
        $files{$File::Find::name} = $distloc;
    }, "$worklib");
    \%files;
}

sub find_script_files {
    my @exe;
    find(sub {
        return if -d && $_ eq 'CVS';
        push @exe, $File::Find::name if -f && /\.pl$/;
    }, 'bin');
    \@exe;
}
